package com.zzb.orders.api;

import com.zzb.orders.model.dto.AddOrderDto;
import com.zzb.orders.model.dto.PayRecordDto;
import com.zzb.orders.service.OrderService;
import com.zzb.orders.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author zzb
 * @Date 2024/7/13 23:41
 * @Version 1.0
 * @description
 */
@Api(value = "订单支付接口", tags = "订单支付接口")
@RestController
@Slf4j
public class OrderController {
    @Resource
    private OrderService orderService;

    @ApiOperation("生成支付二维码")
    @PostMapping("/generatepaycode")
    public PayRecordDto generatePayCode(@RequestBody AddOrderDto addOrderDto) {
        SecurityUtil.XcUser xcUser = SecurityUtil.getUser();
        if (xcUser == null) {
            throw new RuntimeException("请登录后继续选课");
        }
        //获取用户id
        String userId = xcUser.getId();
        //插入订单信息，插入支付记录，生成二维码返回
        return orderService.createOrder(userId, addOrderDto);
    }

    @ApiOperation("扫码下单接口")
    @GetMapping("/requestpay")
    public void requestpay(String payNo, HttpServletResponse response) {
        orderService.requestpay(payNo,response);
    }

    @ApiOperation("查询支付结果")
    @GetMapping("/payresult")
    public PayRecordDto payResult(String payNo) {
        return orderService.queryPayResult(payNo);
    }

    @ApiOperation("支付成功通知")
    @PostMapping("/paynotify")
    public void paynotify(HttpServletRequest request, HttpServletResponse response) {
        orderService.paynotify(request, response);
    }
}
